# -*- coding: utf-8 -*-

"""
Create On: August 08, 2016
@author: corvo

"""

from datetime import datetime
from Basehandler import BaseHandler
from Database.tables import Express
from Utlis.ReturnCodeConfig import CodeTable

class QueryStateHandler(BaseHandler):
    """
    此类为客户端请求查询状态所用, 返回本次代取快递是否结束, 通过手机号码与时间戳确定, 
    查询数据库中的finish, 与receiving字段
    """

    def post(self):
        retjson = {'code': 200, 'content': ''}

        try:
            _user_phone = self.get_argument('user_phone')
            _time_stamp = self.get_argument('sub_time')
            # print (_user_phone)
            # print (_time_stamp)
        except Exception as e:
            print (e)
            retjson['code'] = 300
            retjson['content'] = CodeTable[300]
            self.write_back(retjson)
            return

        try:
            _submit_time = long(_time_stamp) 
            _query_time = datetime.fromtimestamp(_submit_time)
        except Exception as e:
            print (e)
            retjson['code'] = 303
            retjson['content'] = CodeTable[303]
            self.write_back(retjson)
            return 

        exist_info = self.db.query(Express) \
                .filter(Express.phone == _user_phone, \
                        Express.submittime == _query_time)

        try:
            info = exist_info.one()
            res = {'finish': info.finish, 'receiving': info.receiving}
            retjson['content'] = res
        except Exception as e:
            print (e)
            retjson['code'] = 500
            retjson['content'] = CodeTable[500]

        self.write_back(retjson)






